using System;
using System.Reflection;
using System.Resources;
using Microsoft.Office.Core;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using Send2Sharepoint.Common;
using Send2Sharepoint.Forms;
using Send2Sharepoint.Word;

namespace Send2Sharepoint
{
    public partial class Connect
    {
        readonly ResourceManager _rm = new ResourceManager("Send2Sharepoint.Resources", Assembly.GetExecutingAssembly());

        CommandBarButton _uploadDocumentButton;
        CommandBarButton _menuUploadDocumentButton;

        public void RemoveWord()
        {
            _uploadDocumentButton.Delete(Missing.Value);
            _uploadDocumentButton = null;
            _menuUploadDocumentButton.Delete(Missing.Value);
            _menuUploadDocumentButton = null;
        }

        private void CreateWordButtons(CommandBars bars)
        {
            var standardBar = bars["Standard"];
            try
            {
                _uploadDocumentButton = (CommandBarButton)standardBar.Controls[_rm.GetString("ApplicationName")];
                _uploadDocumentButton.Delete(true);
            }
            catch
            {
                //Nothing...
            }
            _uploadDocumentButton = (CommandBarButton)standardBar.Controls.Add(1,
                Missing.Value,
                Missing.Value,
                Missing.Value,
                Missing.Value);
            _uploadDocumentButton.Caption = _rm.GetString("ApplicationName");
            _uploadDocumentButton.Style = MsoButtonStyle.msoButtonIconAndCaption;
            _uploadDocumentButton.Tag = "UploadDocumentButton";
            var bmp = ((Icon)_rm.GetObject("sharepoint")).ToBitmap();
            Clipboard.SetDataObject(bmp, true);
            _uploadDocumentButton.FaceId = 0;
            _uploadDocumentButton.PasteFace();
            _uploadDocumentButton.Click += MenuUploadDocumentButton_Click;
        }

        public void InitializeWord()
        {
            var thisApp = (_Application)Globals.ThisApplication;
            try
            {
                var commandBars = thisApp.CommandBars;
                CreateWordButtons(commandBars);
            }
            catch (Exception ex)
            {
                Logging.WriteException(ex);
                throw;
            }
        }

        void MenuUploadDocumentButton_Click(CommandBarButton ctrl, ref bool cancelDefault)
        {
            try
            {
                var document = ((_Application)Globals.ThisApplication).ActiveDocument;
                if (!string.IsNullOrEmpty(document.Path))
                {
                    var uploadDocument = new UploadWordDocument(document)
                                             {
                                                 Description = "Word document :" + document.Name
                                             };
                    _indexDocument = new IndexDocument(uploadDocument)
                                         {
                                             MinimizeBox = false,
                                             StartPosition = FormStartPosition.CenterScreen
                                         };
                    _indexDocument.ShowDialog();
                }
                else
                {
                    Logging.WriteToLog(string.Format(_rm.GetString("NotSavedMessage"), "Document"));
                    MessageBox.Show(string.Format(_rm.GetString("NotSavedMessage"), "Document"), _rm.GetString("ApplicationName"), MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
            }
            catch (Exception ex)
            {
                Logging.WriteException(ex);
                throw;
            }
        }
    }
}
